<?php

namespace App\Models\Member;

use App\Models\BaseModel;

class GoodPrice extends BaseModel
{
    protected $fillable = [
        'id', 'store_id', 'good_id', 'level',
        'price', 'created_at', 'updated_at'
    ];

    // 传递 店铺ID 和 指定商品iD数组 获取商品价格信息
    public function storeGoodsPrice($storeId, $goodsNewArray, $gradesKeyVal)
    {
        $priceData = $this->whereIn('good_id',$goodsNewArray)->where('store_id', $storeId)
        ->get()->toArray();
        return $this->getGoodsPrice($priceData, $gradesKeyVal);
    }

    // 传递价格数据和会员等级信息 获取前台所需各级商品信息
    private function getGoodsPrice($priceData, $gradesKeyVal)
    {
        $priceArr = [];
        foreach ($priceData as $keyPrice => $valuePrice) {
            $priceData[$keyPrice]['price'] = $valuePrice['price']/100;
            $priceData[$keyPrice]['title'] = $gradesKeyVal[$valuePrice['level']]['title'];
            $priceData[$keyPrice]['jf_num'] = $gradesKeyVal[$valuePrice['level']]['jf_num'];
            $priceArr[$valuePrice['good_id']][$valuePrice['level']] = $priceData[$keyPrice];
        }
        return $priceArr;
    }
}
